package com.geeklan.smart.data.repository.device;

import androidx.annotation.NonNull;

import com.geeklan.smart.data.model.Device;
import com.geeklan.smart.data.remote.RetrofitClient;
import com.geeklan.smart.data.remote.device.DeviceApiService;
import com.geeklan.smart.data.repository.RepositoryCallback;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CmdRepository {
    private final DeviceApiService apiService;

    public CmdRepository() {
        this.apiService = RetrofitClient.getInstance().getCmdApiService();
    }

    public void sendCmd(String userId, RepositoryCallback<Device> callback) {
        Call<Device> call = apiService.sendCMD(userId);
        call.enqueue(new Callback<Device>() {
            @Override
            public void onResponse(@NonNull Call<Device> call, @NonNull Response<Device> response) {
                if (response.isSuccessful() && response.body() != null) {
                    callback.onSuccess(response.body());
                } else {
                    callback.onError(new Exception("Failed to fetch user data")); // 处理错误
                }
            }

            @Override
            public void onFailure(@NonNull Call<Device> call, @NonNull Throwable t) {
                callback.onError(new Exception(t)); // 处理错误
            }
        });
    }
}
